package com.example.a22310.myapplication.Adapter;


import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.a22310.myapplication.Adapter.Drag.RecycleCallBack;

import java.util.List;

public class AddPhotoAdapter extends RecyclerView.Adapter {

    private List<Bitmap> list;

    private RecycleCallBack mRecycleClick;
    public SparseArray<Integer> show = new SparseArray<>();

    public AddPhotoAdapter(RecycleCallBack click, List<Bitmap> data) {
        this.list = data;
        this.mRecycleClick = click;
    }

    public boolean tail(int position) {
        return position == 0;
    }

    public void setData(List<Bitmap> data) {
        this.list = data;
    }

    @Override
    public RecyclerView.ViewHolder  onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {

            return new TailHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_add_picture_header, null),mRecycleClick);
        } else {

            return new DragHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_add_picture, parent, false),mRecycleClick);
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        if (tail(position)) {
            //大家在这里面处理头，这里只有一个TextView，大家可以根据自己的逻辑做修改
            ((TailHolder)viewHolder).getImageView().setImageResource(R.drawable.ic_add_picture);
        }else {
            //其他条目中的逻辑在此
            ((DragHolder)viewHolder).getImageView().setImageBitmap(list.get(position-1));
        }



    }

    @Override
    public int getItemCount() {
        return list.size()+1;
    }

    public int getItemViewType(int position) {
        //如果是0，就是头，否则则是其他的item
        return tail(position) ? 0 : 1;
    }


    public class DragHolder extends RecyclerView.ViewHolder implements
            View.OnClickListener,View.OnLongClickListener,View.OnTouchListener{

        public LinearLayout item;
        private RecycleCallBack mClick;
        public ImageView imageView;

        public DragHolder(View itemView, RecycleCallBack click) {
            super(itemView);
            mClick = click;
            item = (LinearLayout) itemView.findViewById(R.id.item);
            imageView = (ImageView) itemView.findViewById(R.id.imageView);
            item.setOnClickListener(this);
            item.setOnLongClickListener(this);
            item.setOnTouchListener(this);


        }
        public ImageView getImageView() {
            return imageView;
        }



        @Override
        public void onClick(View v) {
            if (null != mClick) {
                show.clear();
                mClick.itemOnClick(getAdapterPosition(), v);
            }
        }


        @Override
        public boolean onLongClick(View view) {
            if (null != mClick) {
                show.clear();
                // mClick.itemOnLongClick(getAdapterPosition(), view);
            }
            return false;
        }

        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if (null != mClick) {
                show.clear();
                mClick.onTouch(view,motionEvent);
            }
            return false;
        }

    }

    public class TailHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        ImageView imageView;
        private RecycleCallBack mClick;
        public LinearLayout item;
        public TailHolder(View itemView, RecycleCallBack click) {
            super(itemView);
            mClick = click;
            imageView = (ImageView)itemView.findViewById(R.id.header_choice_dialog);
            item = (LinearLayout) itemView.findViewById(R.id.header_item);
            item.setOnClickListener(this);

        }
        public ImageView getImageView() {
            return imageView;
        }

        @Override
        public void onClick(View view) {
            if (null != mClick) {
                show.clear();
                mClick.itemOnClick(getAdapterPosition(), view);
            }
        }
    }

}
